package com.bocai.yuweiwangluo.base;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.event.AsyncEvent;
import com.bocai.yuweiwangluo.event.BackgroundEvent;
import com.bocai.yuweiwangluo.event.MainEvent;
import com.bocai.yuweiwangluo.event.PostEvent;
import com.bocai.yuweiwangluo.utils.MyProgressUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import de.greenrobot.event.EventBus;


/**
 * Created by kaizen on 15/10/21.
 */
public abstract class BaseActivity extends FragmentActivity implements BaseView
{
    private Dialog mDialog;
    protected Toolbar mToolbar;
    protected TextView mTitleTv;
    @Override
       protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(contentView());
        EventBus.getDefault().register(this);
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this));
        mDialog= MyProgressUtil.createLoadingDialog(this,"请稍后...");
        mDialog.setCanceledOnTouchOutside(false);
        setData();

    }
    protected abstract int contentView();

    protected abstract void setData(
    );

    protected void initEvent()
    {

    }

    protected void initToolbar(String title) {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mTitleTv.setText(title);
        mToolbar.setTitle("");
        mToolbar.setNavigationIcon(R.mipmap.title_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    /**
     * 在发布线程运行
     * @param event
     */
    public void onEvent(PostEvent event) {
    }

    /**
     * 在主线程运行
     * @param event
     */
    public void onEventMainThread(MainEvent event) {
    }

    /**
     * 如果是主线程发布，则在子线程中运行，否则在发布线程运行
     * @param event
     */
    public void onEventBackgroundThread(BackgroundEvent event) {
    }

    /**
     * 总是新建子线程
     * @param event
     */
    public void onEventAsync(AsyncEvent event) {
    }
    @Override
    protected void onStart()
    {
        super.onStart();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out); //由右向左滑入的效果
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
        overridePendingTransition(R.anim.slide_right_in,R.anim.slide_left_out); //由右向左滑入的效果
    }

    @Override
    public void showLoading()
    {
        mDialog.show();
    }

    @Override
    public void hideLoading()
    {
        mDialog.dismiss();
    }

    @Override
    public void showError(String msg)
    {
        Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setData(String json)
    {

    }
}
